public class HashBuck {
//    //创建哈希节点
    static class Node {
        public int key;
        public int val;
        public Node next;

        public Node(int key, int val) {
            this.key = key;
            this.val = val;
        }
    }
//        //创建哈希数组
        public Node[] array = new  Node[10];
        public int usedSize;
        public static final double  LOAD_FACTOR = 0.75;
//        /**
//         * 插入元素
//         */
        public void push(int key,int val) {
            Node node = new Node(key,val);
            int index = key % array.length;
            //1.判断放在那个位置
            Node cur = array[index];
            //2.进行插入
//            //为空
//            if(cur == null) {
//                array[index] = node;
//                usedSize++;
//                return;
//            }
            //不为空
            while(cur != null) {
                if(cur.key == key) {
                    cur.val = val;
                    return;
                }
                cur = cur.next;
            }
            //头插法
            node.next = array[index];
            array[index] = node;
            usedSize++;
            //3.判断是否超过阀值
            if(doLoadFactor() >= LOAD_FACTOR) {
                //处理哈希表
                reSize();
            }
        }
//        //处理哈希表,重新哈希
        private void reSize() {
            //扩大数组
            Node[] newArray = new Node[2 * array.length];
            //重新哈希
            //遍历新数组
            for(int i = 0;i < array.length;i++) {
                //重新进行判断Node数组每个位置里的值
                Node cur = array[i];
                while(cur != null) {
                    //记录cur下一个节点
                    Node curNext = cur.next;
                    //找到数组内值的新位置
                    int index = cur.key % newArray.length;
                    //进行头插
                    cur.next = newArray[index];
                    newArray[index] = cur;
                    cur = curNext;
                }
            }
            //把新数组赋值给原来数组
            array = newArray;
        }
        //判断阀值
        private double doLoadFactor() {
            return usedSize * 1.0 / array.length;
        }
    /**
     * 查找key
     */
    public int get(int key) {
        int index = key % array.length;
        Node cur = array[index];
        //循环查找
        while(cur != null) {
            if(cur.key == key) {
                return cur.val;
            }
        }
        return -1;
    }
}